package cn.jbt.api.admin.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "sys_dept", schema = "jbt")
public class SysDept {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @NotBlank(message = "部门名称不能为空")
    @Column(name = "name", nullable = false, length = 200)
    private String name;

    @Column(name = "node", nullable = false)
    private String node = "";

    @Column(name = "floor", nullable = false)
    private Integer floor = 1;

    @Column(name = "phone", nullable = false, length = 20)
    private String phone = "";

    @Column(name = "email", nullable = false, length = 200)
    private String email = "";

    @NotNull(message = "排序属性不能为空")
    @Min(value = 0, message = "排序属性'值'错误")
    @Max(value = 1000000000, message = "排序属性'值'错误")
    @Column(name = "orderby", nullable = false)
    private Integer orderby;

    @NotNull(message = "当前状态不能为空")
    @Min(value = 0, message = "当前状态'值'错误")
    @Max(value = 1, message = "当前状态'值'错误")
    @Column(name = "status", nullable = false)
    private Integer status;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    @Column(name = "create_by", nullable = false, length = 64)
    private String createBy = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    @Column(name = "update_by", nullable = false, length = 64)
    private String updateBy = "";

    @Column(name = "remark", nullable = false)
    private String remark = "";

    @Min(value = 0, message = "所属父级'值'错误")
    @Column(name = "pid", nullable = false)
    private Long pid = 0L;

    /* 部门负责人 */
    @Transient
    private List<Long> headIds;

    public void edit(SysDept sysDept) {
        this.name = sysDept.name;
        this.orderby = sysDept.orderby;
        this.status = sysDept.status;
        this.phone = sysDept.phone;
        this.email = sysDept.email;
        this.remark = sysDept.remark;
    }
}
